<?php
/**
 * Created by PhpStorm.
 * User: boye009
 * Date: 2016/2/27
 * Time: 10:07
 */

namespace Shop\Api;

use Common\Api\Api;
use Shop\Model\FavoritesModel;

class FavoritesApi extends Api{

    /**
     * 添加
     */
    const ADD = "Shop/Favorites/add";


    /**
     * 查询一条数据
     */
    const GET_INFO = "Shop/Favorites/getInfo";

    /**
     * 删除
     */
    const DELETE = "Shop/Favorites/delete";

    /**
     * 统计
     */
    const COUNT = "Shop/Favorites/count";
    /**
     * 查看收藏帖子
     */
    const QUERY_THREAD_WITH_COUNT = "Shop/Favorites/queryThreadWithCount";

    const QUERY_PRODUCT_WITH_COUNT = "Shop/Favorites/queryProductWithCount";

    protected function _init(){
        $this -> model = new FavoritesModel();
    }

    public function queryProductWithCount($map = null, $page = array('curpage'=>0,'size'=>10), $order = false, $params = false, $fields = false){

        $query = $this -> model;

        $main_img = 6015;

        $query = $query -> join("(select id,name,onshelf,status from itboye_product) as product on favorite_id=product.id") -> join("(select pid,img_id from itboye_product_image where type=".$main_img.") as image on favorite_id=image.pid","left") -> join("(select product_id,min(price) as price from itboye_product_sku group by product_id) as sku on favorite_id=sku.product_id","left");

        $query = $query -> page($page['curpage'] . ',' . $page['size']);

        if(!is_null($map)){
            $map['status'] = 1;
            $map['onshelf'] = 1;
            $query = $query -> where($map);
        }
        if(!($order === false)){
            $query = $query->order($order);
        }
        if(!($fields === false)){
            $query = $query->field($fields);
        }
        $list = $query -> select();

        if ($list === false) {
            $error = $this -> model -> getDbError();
            return $this -> apiReturnErr($error);
        }


        $count = $this -> model -> join("(select id,name,onshelf,status from itboye_product) as product on favorite_id=product.id") -> join("(select pid,img_id from itboye_product_image where type=".$main_img.") as image on favorite_id=image.pid","left") -> join("(select product_id,min(price) as price from itboye_product_sku group by product_id) as sku on favorite_id=sku.product_id","left") -> where($map) -> count();
        // 查询满足要求的总记录数
        $Page = new \Think\Page($count, $page['size']);

        //分页跳转的时候保证查询条件
        if ($params !== false) {
            foreach ($params as $key => $val) {
                $Page -> parameter[$key] = urlencode($val);
            }
        }

        // 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page -> show();

        return $this -> apiReturnSuc(array("count" => $count, "list" => $list));

    }


    public function queryThreadWithCount($map = null, $page = array('curpage'=>0,'size'=>10), $order = false, $params = false, $fields = false){

        $query = $this->model;

        $query = $query -> join("itboye_forum_thread on favorite_id=itboye_forum_thread.tid");

        $query = $query -> page($page['curpage'] . ',' . $page['size']);

        if(!is_null($map)){
            $map['status'] = 1;
            $query = $query -> where($map);
        }
        if(!($order === false)){
            $query = $query->order($order);
        }
        if(!($fields === false)){
            $query = $query->field($fields);
        }
        $list = $query -> select();

        if ($list === false) {
            $error = $this -> model -> getDbError();
            return $this -> apiReturnErr($error);
        }

        $count = $this -> model -> join("itboye_forum_thread on favorite_id=itboye_forum_thread.tid") -> where($map) -> count();
        // 查询满足要求的总记录数
        $Page = new \Think\Page($count, $page['size']);

        //分页跳转的时候保证查询条件
        if ($params !== false) {
            foreach ($params as $key => $val) {
                $Page -> parameter[$key] = urlencode($val);
            }
        }

        // 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page -> show();

        return $this -> apiReturnSuc(array("count" => $count, "list" => $list));

    }

}